#!/bin/sh

render() {
	time=$(date '+%H:%M')
	hour=$(echo "$time" | cut -d ':' -f1 | sed -E 's/^0//')
	hour=$((hour % 12))
	minute=$(echo "$time" | cut -d ':' -f2 | sed -E 's/^0//')
	minute=$((minute / 5))

	if [ "$hour" -gt "$minute" ]
	then
		colour_bg='#e7e9f3'
		colour_fg='#db9d3b'
		short="$minute"
		long="$hour"
	elif [ "$hour" -eq "$minute" ]
	then
		colour_bg='#317b9f'
		colour_fg='#317b9f'
		short="$minute"
		long="$hour"
	else
		colour_bg='#db9d3b'
		colour_fg='#e7e9f3'
		short="$hour"
		long="$minute"
	fi

	clock="</span>"
	i=1; while [ $i -le $short ]
	do
		if [ $(( i % 2 )) -eq 0 ]
		then
			block="█"
		else
			block="▓"
		fi
		clock="$block$clock"
		if [ $(( i % 3 )) -eq 0 ]
		then
			clock=" $clock"
		fi
		i=$(( i + 1 ))
	done
	clock="</span><span color=\"$colour_fg\">$clock"
	while [ $i -le $long ]
	do
		if [ $(( i % 2 )) -eq 0 ]
		then
			block="█"
		else
			block="▓"
		fi
		clock="$block$clock"
		if [ $(( i % 3 )) -eq 0 ]
		then
			clock=" $clock"
		fi
		i=$(( i + 1 ))
	done
	clock="<span color=\"$colour_bg\">$clock"
	while [ $i -le 12 ]
	do
		clock=" $clock"
		if [ $(( i % 3 )) -eq 0 ]
		then
			clock=" $clock"
		fi
		i=$(( i + 1 ))
	done
	printf '%s' "$clock"
}

if [ -e /tmp/swaybar/pipe ]
then
	printf "clock|%s\n" "$(render)" >/tmp/swaybar/pipe
fi
